package com.example.demo.dao.warehouse;

import com.example.demo.model.warehouse.Product;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface ProductDAO {
    //    一对一查询全部
    @Select("select * from product")
    @Result(column = "employee_id", property = "employee",
            one = @One(select = "com.example.demo.dao.people.EmployeeDAO.getByEmployeeId",fetchType = FetchType.LAZY))
    @Result(column = "id", property = "provider",
            one = @One(select = "com.example.demo.dao.people.ProviderDAO.getByProviderId",fetchType = FetchType.LAZY))
    @Result(column = "product_model", property = "dict1",
            one = @One(select = "com.example.demo.dao.system.DictDAO.getByDictModelId",fetchType = FetchType.LAZY))
    @Result(column = "product_unit", property = "dict2",
            one = @One(select = "com.example.demo.dao.system.DictDAO.getByDictUnitId",fetchType = FetchType.LAZY))
    public List<Product> selectAll();
    //    添加数据
    @Insert("insert into product(product_name,employee_id,product_place,product_price,product_model,product_unit,id)values(#{product_name},#{employee_id},#{product_place},#{product_price},#{product_model},#{product_unit},#{id})")
    Integer addProduct(Product product);
    //删除数据
    @Delete("delete from product where product_id=#{product_id}")
    Integer deleteById(Integer product_id);
    //    修改数据
    @Update("update product set product_name=#{product_name},employee_id=#{employee_id},product_place=#{product_place},product_price=#{product_price},product_model=#{product_model},product_unit=#{product_unit},id=#{id} where product_id=#{product_id} ")
    Integer updateById(Product product);

    @Update("update product set product_status=1 where product_id=#{product_id}")
    Integer updateStatusById(Integer product_id);
    //  模糊查询
    @Select("<script>"+
            "select * from product" +
            "<where>"+
            "<if test=\"product_name !=null and product_name != ''\">"+
            "and product_name like CONCAT('%',#{product_name},'%')"+
            "</if>"+
            "<if test=\"id !=null and id != ''\">"+
            "and id like CONCAT('%',#{id},'%')"+
            "</if>"+
            "<if test=\"product_model !=null and product_model != ''\">"+
            "and product_model like CONCAT('%',#{product_model},'%')"+
            "</if>"+
            "<if test=\"product_unit !=null and product_unit != ''\">"+
            "and product_unit like CONCAT('%',#{product_unit},'%')"+
            "</if>"+
            "</where>"+
            "</script>")
    @Result(column = "employee_id", property = "employee",
            one = @One(select = "com.example.demo.dao.people.EmployeeDAO.getByEmployeeId",fetchType = FetchType.LAZY))
    @Result(column = "id", property = "provider",
            one = @One(select = "com.example.demo.dao.people.ProviderDAO.getByProviderId",fetchType = FetchType.LAZY))
    @Result(column = "product_model", property = "dict1",
            one = @One(select = "com.example.demo.dao.system.DictDAO.getByDictModelId",fetchType = FetchType.LAZY))
    @Result(column = "product_unit", property = "dict2",
            one = @One(select = "com.example.demo.dao.system.DictDAO.getByDictUnitId",fetchType = FetchType.LAZY))
    public List<Product> findProduct(String product_name,Integer id,Integer product_model,Integer product_unit) ;

//    一对一
    @Select("select * from product where product_id=#{product_id}")
    List<Product> getByProductId(Integer product_id);

//    查询数据
    @Select("select * from product")
    public List<Product> All();
}
